package com.guogee.pdb.dao;

import com.guogee.pdb.bean.SmartLock;
import com.guogee.pdb.db.DBHelper.SmartLockCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class SmartLockDAO extends ADao<SmartLock>{
	
	

	public SmartLockDAO() {
		super();
		dbCollection = dbHelper.getIsmartDB().getCollection(SmartLockCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(SmartLock obj) {
		if(insertItem1(obj) != null){
			return true;
		}
		return false;
	}

	@Override
	public SmartLock parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());
	}

	@Override
	public String insertItem1(SmartLock obj) {
		// TODO Auto-generated method stub
		DBObject lock = new BasicDBObject();
		lock.put(SmartLockCollection.USER_NAME, obj.getUserName());
		lock.put(SmartLockCollection.DEVICE_ADDR, obj.getDeviceAddr());
		lock.put(SmartLockCollection.LOCK_ID,obj.getLockId());
		if(checkWritenResult(dbCollection.insert(lock))){
			return lock.get(SmartLockCollection.ID).toString();
		}
		return null;
	}

	@Override
	public SmartLock parstDBObject(DBObject object) {
		// TODO Auto-generated method stub
		SmartLock smartLock=new SmartLock();
		smartLock.setId(object.get(SmartLockCollection.ID).toString());
		smartLock.setDeviceAddr(object.get(SmartLockCollection.DEVICE_ADDR).toString());
		smartLock.setUserName(object.get(SmartLockCollection.USER_NAME).toString());
		smartLock.setLockId(Integer.parseInt(object.get(SmartLockCollection.LOCK_ID).toString()));
		return smartLock;
	}

	
}
